﻿#include "stdafx.h"
#include "GaussianBlurTask.h"
#include <QVBoxLayout>
#include <QSpinBox>
#include <QDoubleSpinBox>
#include <QPushButton>
#include <QLabel>

void GaussianBlurTask::execute(cv::Mat& image) {
	if (kernelSize % 2 == 0) kernelSize++; // 确保核大小为奇数
	cv::GaussianBlur(image, image, cv::Size(kernelSize, kernelSize), sigmaX);
}

std::string GaussianBlurTask::getName() const {
	return "高斯模糊";
}

std::unique_ptr<QDialog> GaussianBlurTask::getConfigUI() {
	auto dialog = std::make_unique<QDialog>();
	QVBoxLayout* layout = new QVBoxLayout(dialog.get());

	// 核大小输入
	QSpinBox* kernelSpin = new QSpinBox();
	kernelSpin->setRange(1, 99);
	kernelSpin->setSingleStep(2); // 步长为 2，保证奇数
	kernelSpin->setValue(kernelSize);
	layout->addWidget(new QLabel("核大小（奇数）:"));
	layout->addWidget(kernelSpin);

	// sigmaX 输入
	QDoubleSpinBox* sigmaSpin = new QDoubleSpinBox();
	sigmaSpin->setRange(0.0, 100.0);
	sigmaSpin->setValue(sigmaX);
	layout->addWidget(new QLabel("sigmaX:"));
	layout->addWidget(sigmaSpin);

	// 确认按钮
	QPushButton* okButton = new QPushButton("确认");
	layout->addWidget(okButton);

	QObject::connect(okButton, &QPushButton::clicked, dialog.get(), [&, kernelSpin, sigmaSpin]() {
		kernelSize = kernelSpin->value();
		sigmaX = sigmaSpin->value();
		dialog->accept();
		});

	return dialog;
}